package cn.itsource.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.annotation.Log;
import cn.itsource.system.domain.Employee;
import cn.itsource.system.domain.Logininfo;
import cn.itsource.system.domain.Role;
import cn.itsource.system.mapper.EmployeeMapper;
import cn.itsource.system.mapper.LogininfoMapper;
import cn.itsource.system.query.EmployeeQuery;
import cn.itsource.system.service.IEmployeeService;
import cn.itsource.user.domain.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：菜单Employee
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }

    @Override
    public Employee findOne(Long id) {
        return employeeMapper.findOne(id);
    }

    @Transactional
    @Override
    @Log
    public void add(Employee employee) {
        //1.处理盐值和密码
        //1.1.生成随机的盐值
        String salt = RandomUtil.randomString(32);
        //1.2.使用工具类加密加盐  Md5()
        String md5Pwd = SecureUtil.md5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //2.添加登录信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);//根据属性同名原则拷贝
        logininfo.setType(0);
        if(employee.getState()==0){
            logininfo.setDisable(false);
        }
        logininfoMapper.add(logininfo);

        //3.添加员工信息
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.add(employee);

        //添加员工的角色信息
        if(employee.getRoles().size()>0){
            employeeMapper.addEmpRole(employee);
        }
    }

    @Transactional
    @Override
    @Log
    public void update(Employee employee) {
        Employee oldEmp = employeeMapper.findOne(employee.getId());

        //修改员工信息
        employeeMapper.update(employee);
        //修改员工的角色信息
        employeeMapper.delEmpRole(employee.getId());//删除以前员工的久角色
        //添加员工的角色信息
        if(employee.getRoles().size()>0){
            employeeMapper.addEmpRole(employee);
        }

        //修改登录信息
        Logininfo logininfo = new Logininfo();
        //id注意: id,phone,email
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setId(oldEmp.getLogininfoId());

        logininfoMapper.update(logininfo);
        if(employee.getState()==0){
            logininfo.setDisable(false);
        }
    }

    @Transactional
    @Override
    @Log
    public void delete(Long id) {
        Employee employee = employeeMapper.findOne(id);

        //删除员工角色信息 - 有外键，先删关联数据，再删主表数据
        employeeMapper.delEmpRole(id);
        //删除员工信息
        employeeMapper.delete(id);

        //删除登录信息
        logininfoMapper.delete(employee.getLogininfoId());
    }

    @Transactional
    @Override
    @Log
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public PageInfo<Employee> queryPage(EmployeeQuery employeeQuery) {
        //开启分页
        PageHelper.startPage(employeeQuery.getCurrentPage(),employeeQuery.getPageSize());
        //1.首先会查询总条数
        //2.会根据方言或驱动名进行分页查询数据 - mysql - limit
        //3.会自动计算limit的第一个参数

        List<Employee> list = employeeMapper.queryPage(employeeQuery);
        for (Employee employee : list) {
            List<Role> dbRoles = employeeMapper.getEmpRoles(employee.getId());
            employee.setDbRoles(dbRoles);
        }

        //调用PageInfo的有参会计算其他参数
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(list);
        return pageInfo;
    }

}
